package com.gandong8.hbase.web.controller;

import com.gandong8.hbase.tool.domain.HbaseQuery;
import com.gandong8.hbase.tool.service.HbaseServiceInter;
import com.gandong8.hbase.web.domain.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


@Slf4j
@Controller
@RequestMapping(value = "/record",produces = "application/json;charset=UTF-8")
public class RecordController {
    @Autowired
    HbaseServiceInter hbaseServiceInter;

    @RequestMapping(value = { "", "/" })
    @ResponseBody
    public ModelAndView list(String tableName) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("pageTitle", "数据管理");
        modelAndView.addObject("tableList", hbaseServiceInter.listTable());
        modelAndView.addObject("tableName", tableName==null ? "":tableName);
        modelAndView.setViewName("hbase/record");
        return modelAndView;
    }

    @RequestMapping(value = "/ajax/add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(String frmTableName, String frmRowKey,String[] frmColumn, String[] frmColumnVal) {
        return Result.run(hbaseServiceInter.save(frmTableName, frmRowKey, frmColumn, frmColumnVal));
    }

    @RequestMapping(value = "/ajax/row/del")
    @ResponseBody
    public Result rowDel(String tableName, String rowKeys) {
        if(tableName == null || rowKeys == null) {
            return Result.failed();
        }
        List<String> arr = Arrays.asList(rowKeys.split(","));
        return Result.run(hbaseServiceInter.delete(tableName, arr));
    }

    @RequestMapping(value = "/ajax/column/del")
    @ResponseBody
    public Result columnDel(String tableName, String rowKey, String column) {
        if(tableName == null || rowKey == null || column == null) {
            return Result.failed();
        }
        String[] arr = column.split(":");
        return Result.run(hbaseServiceInter.deleteColumn(tableName, rowKey, arr[0], arr[1]));
    }

    @RequestMapping(value = "/ajax/column/update", method = RequestMethod.POST)
    @ResponseBody
    public Result columnUpdate(String tableName, String rowKey, String column, String value) {
        if(tableName == null || rowKey == null || column == null || value == null) {
            return Result.failed();
        }
        return Result.run(hbaseServiceInter.save(tableName, rowKey, new String[] {column}, new String[] {value}));
    }

    @RequestMapping(value = "/ajax/list")
    @ResponseBody
    public Result list(String tableName, String key, String dateRange) {
        if(StringUtils.isBlank(tableName)) {
            return Result.failed();
        }
        HbaseQuery query = new HbaseQuery();
        if(StringUtils.isNotEmpty(key)) {
            query.setRowKey(key);
        }
        if(StringUtils.isNotEmpty(dateRange)) {
            try {
                String[] arr = dateRange.split("-");
                query.setMinStamp(DateUtils.parseDate(arr[0].trim(), new String[] { "yyyy/MM/dd" }).getTime());
                query.setMaxStamp(DateUtils.parseDate(arr[1].trim(), new String[] { "yyyy/MM/dd" }).getTime());
            } catch (ParseException e) {
                return Result.failed("开始时间格式不对");
            }
        }
        return Result.success(hbaseServiceInter.search(tableName, query));
    }
}
